from huey.exceptions import TaskException

from src.edit_file.datamodel import AiEditModel
from src.edit_file.edit_SOPfile import EditSOPFile
from src.edit_file.edit_BPRfile import EditBPRFile
from src.huey_init._huey import huey
from src.tools.dms_operator import dmsoperator
from src.tools.file_type import check_file_type1


@huey.task(context=True)
def run_edit_task(edit_model: AiEditModel, task=None):
    diff_file_info = edit_model.diff_info  # 信息id
    revise_files_list = diff_file_info["revise_file_list"]
    template_file = diff_file_info["template_file"]
    # 根据文件名称和文件内容判断修订工艺规程还是批记录
    if "工艺规程" in template_file["documentName"] or "工艺规程" in check_file_type1(template_file["realPath"]):
        run_editsop_task(edit_model, revise_files_list, template_file, task)
    elif "记录" in template_file["documentName"] or "生产批记录" in check_file_type1(template_file["realPath"]):
        run_editbpr_task(edit_model, revise_files_list, template_file, task)
    else:
        errorinfo = {
            "FunctionType": edit_model.exchange_info.get("functionType"),
            "TaskId": edit_model.exchange_info.get("dmsTaskId"),
            "AiTaskId": task.id,
            "Reason": "没有工艺规程或批记录"
        }
        dmsoperator.post_errortodms(errorinfo=errorinfo)
        raise TaskException("没有工艺规程或批记录")


def run_editsop_task(edit_model: AiEditModel, revise_files_list, template_file, task=None):
    aiedit_sopfile = EditSOPFile()
    for revise_file in revise_files_list:
        # 修订工艺规程入口
        res = aiedit_sopfile.edit_file(revise_file, template_file["realPath"], task.id)
        exchange_info = {
            "functionType": edit_model.exchange_info.get("functionType"),
            "taskType": edit_model.exchange_info.get("taskType"),
            "taskDocumentType": edit_model.exchange_info.get("taskDocumentType"),
            "executionType": edit_model.exchange_info.get("executionType"),
            "isSuccess": True,
            "dmsTaskId": edit_model.exchange_info.get("dmsTaskId"),
            "docId": revise_file.get("docId"),
            "taskDocId": revise_file.get("taskDocId"),
            "resultFirstFilePath": res,
            "resultSecondFilePath": None,
            "aiTaskId": task.id,
            "aiGenFileType": 0,
        }
        dms_res = dmsoperator.post_exchange_info(exchange_info)
        if '"code":500' in dms_res:
            errorinfo = {
                "FunctionType": edit_model.exchange_info.get("functionType"),
                "TaskId": edit_model.exchange_info.get("dmsTaskId"),
                "AiTaskId": task.id,
                "Reason": "修订文件编辑失败"
            }
            dmsoperator.post_errortodms(errorinfo=errorinfo)
            raise TaskException("修订文件编辑失败")


def run_editbpr_task(edit_model: AiEditModel, revise_files_list, template_file, task=None):
    aiedit_bprfile = EditBPRFile()
    for revise_file in revise_files_list:
        # 修订批记录入口
        res = aiedit_bprfile.edit_file(revise_file, template_file["realPath"], task.id)
        exchange_info = {
            "functionType": edit_model.exchange_info.get("functionType"),
            "taskType": edit_model.exchange_info.get("taskType"),
            "taskDocumentType": edit_model.exchange_info.get("taskDocumentType"),
            "executionType": edit_model.exchange_info.get("executionType"),
            "isSuccess": True,
            "dmsTaskId": edit_model.exchange_info.get("dmsTaskId"),
            "docId": revise_file.get("docId"),
            "taskDocId": revise_file.get("taskDocId"),
            "resultFirstFilePath": res,
            "resultSecondFilePath": None,
            "aiTaskId": task.id,
            "aiGenFileType": 0,
        }
        dms_res = dmsoperator.post_exchange_info(exchange_info)
        if '"code":500' in dms_res:
            errorinfo = {
                "FunctionType": edit_model.exchange_info.get("functionType"),
                "TaskId": edit_model.exchange_info.get("dmsTaskId"),
                "AiTaskId": task.id,
                "Reason": "修订文件编辑失败"
            }
            dmsoperator.post_errortodms(errorinfo=errorinfo)
            raise TaskException("修订文件编辑失败")